package com.library.service.impl;/*
 *@author lzg
 *@data 2020/11/18
 */

import com.library.entity.Book;
import com.library.entity.BorrowRecode;
import com.library.repository.BookRepository;
import com.library.repository.BorrowRepository;
import com.library.repository.impl.BookRepositoryImpl;
import com.library.repository.impl.BorrowRepositoryImpl;
import com.library.service.BookService;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class BookServiceImpl implements BookService {
    BookRepository bookRepository = new BookRepositoryImpl();
    BorrowRepository borrowRepository = new BorrowRepositoryImpl();
    @Override
    public List<Book> findAll(Integer page) {
        int limitNu = 6;
        return bookRepository.findAll(page, limitNu);
    }

    //获取总页数
    @Override
    public int getPageSize(Integer limitNu){
        int books = bookRepository.getBooks();
        int pageSize = 0;
        if (books%limitNu==0){
            pageSize = books/limitNu;
        }else {
            pageSize = (books/limitNu)+1;
        }
        return pageSize;
    }

    @Override
    public void addBorrow(Integer bookId, Integer readerId) {
        //借书时间
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyy-MM-dd");
        String borrowTime = format.format(date);
        //还书时间，14天后
        Calendar  calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR,Calendar.DAY_OF_YEAR+14);
        Date back_date = calendar.getTime(); //获取14天后的日期
        String return_time = format.format(back_date);
        borrowRepository.insertData(bookId,readerId,borrowTime,return_time,null);
    }

    @Override
    public List<BorrowRecode> findAllBorrowRecodeByReaderId(Integer readerId) {
        return borrowRepository.findAllBorrowRecodeByReaderId(readerId);
    }

}
